import sys


class Basic:
    @staticmethod
    def main():
        Basic.literal_string()
        Basic.literal_numeric()
        Basic.literal_bool()
        Basic.literal_operators()

    @staticmethod
    def literal_string():
        # 字节字符串(只允许 ascii 码字符)
        string_bytes: str = b'bytes literals'
        print(string_bytes)

        # 字符串
        string_text: str = 'string - 字符串'
        print(string_text.encode('utf-8', 'ignore').decode(sys.stdout.encoding, 'ignore'))

        # 字符串
        string_raw: str = r'raw string - \n 原始字符串'
        print(string_raw.encode('utf-8', 'ignore').decode(sys.stdout.encoding, 'ignore'))

        # 格式化字符串(formatted string literals)
        string_formatted: str = f"text: {string_text}"
        print(string_formatted.encode('utf-8', 'ignore').decode(sys.stdout.encoding, 'ignore'))

    @staticmethod
    def literal_numeric():
        # 整数
        numeric_int: int = 100
        print(numeric_int)

        # 浮点数
        numeric_float: float = 1.123456789
        print(numeric_float)

        # 复数
        numeric_imaginary: complex = 1 + 1j
        print(numeric_imaginary)

    @staticmethod
    def literal_bool():
        # 布尔
        bool_var: bool = False
        print(bool_var)
        print(not bool_var)

    @staticmethod
    def literal_operators():
        x = 10
        y = 3
        # 加
        print(f'{x} + {y} =', x + y)
        # 减
        print(f'{x} - {y} =', x - y)
        # 乘
        print(f'{x} * {y} =', x * y)
        # 乘方
        print(f'{x} ** {y} =', x**y)
        # 除
        print(f'{x} / {y} =', x / y)
        # 除（向下取整）
        print(f'{x} // {y} =', x // y)
        # 余
        print(f'{x} % {y} =', x % y)

        # 右位移
        print(f'8 >> 1 =', 8 >> 1)
        # 左位移
        print(f'8 << 1 =', 8 << 1)
        # 位与
        print(f'8 & 9 =', 8 & 9)
        # 位或
        print(f'8 | 1 =', 8 | 1)
        # 位异或
        print(f'8 ^ 1 =', 8 ^ 1)
        # 位非
        print(f'~8 =', ~8)

        # 自加
        x_origin = x
        x += y
        print(f'{x_origin} += {y} =', x)
        # 自减
        x = x_origin
        x -= y
        print(f'{x_origin} -= {y} =', x)
        # 自乘
        x = x_origin
        x *= y
        print(f'{x_origin} *= {y} =', x)
        # 除
        x = x_origin
        x /= y
        print(f'{x_origin} / {y} =', x)
        # 除（向下取整）
        x = x_origin
        x //= y
        print(f'{x_origin} //= {y} =', x)
        # 余
        x = x_origin
        x %= y
        print(f'{x_origin} %= {y} =', x)
        # 自乘方
        x = x_origin
        x **= y
        print(f'{x_origin} **= {y} =', x)

        # 矩阵乘法(需要实现内置方法 __matmul__() )
        A = Matrix([[1, 2], [3, 4]])
        B = Matrix([[5, 6], [7, 8]])
        print(f'multiply matrices =', A @ B)


class Matrix(list):
    def __matmul__(self, B):
        A = self
        return Matrix(
            [
                [sum(A[i][k] * B[k][j] for k in range(len(B))) for j in range(len(B[0]))]
                for i in range(len(A))
            ]
        )


if __name__ == "__main__":
    Basic.main()
